package cn.iocoder.yudao.module.system.controller.admin.suggestion;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.api.user.MemberUserApi;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionRespVO;
import cn.iocoder.yudao.module.system.controller.admin.suggestion.vo.SuggestionSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserUpdateStatusReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.suggestion.SuggestionDO;
import cn.iocoder.yudao.module.system.service.suggestion.SuggestionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 意见建议")
@RestController
@RequestMapping("/system/suggestion")
@Validated
public class SuggestionController {

    @Resource
    private SuggestionService suggestionService;
    @Resource
    private MemberUserApi memberUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建意见建议")
    @PreAuthorize("@ss.hasPermission('system:suggestion:create')")
    public CommonResult<Long> createSuggestion(@Valid @RequestBody SuggestionSaveReqVO createReqVO) {
        return success(suggestionService.createSuggestion(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新意见建议")
    @PreAuthorize("@ss.hasPermission('system:suggestion:update')")
    public CommonResult<Boolean> updateSuggestion(@Valid @RequestBody SuggestionSaveReqVO updateReqVO) {
        suggestionService.updateSuggestion(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除意见建议")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:suggestion:delete')")
    public CommonResult<Boolean> deleteSuggestion(@RequestParam("id") Long id) {
        suggestionService.deleteSuggestion(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得意见建议")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:suggestion:query')")
    public CommonResult<SuggestionRespVO> getSuggestion(@RequestParam("id") Long id) {
        SuggestionDO suggestion = suggestionService.getSuggestion(id);
        return success(BeanUtils.toBean(suggestion, SuggestionRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改合伙人状态")
    @PreAuthorize("@ss.hasPermission('system:suggestion:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody UserUpdateStatusReqVO reqVO) {
        suggestionService.updateStatus(reqVO.getId(), reqVO.getStatus());
        return success(true);
    }

    @GetMapping("/page")
    @Operation(summary = "获得意见建议分页")
    @PreAuthorize("@ss.hasPermission('system:suggestion:query')")
    public CommonResult<PageResult<SuggestionRespVO>> getSuggestionPage(@Valid SuggestionPageReqVO pageReqVO) {
        PageResult<SuggestionDO> pageResult = suggestionService.getSuggestionPage(pageReqVO);
        PageResult<SuggestionRespVO> pageResulVO = BeanUtils.toBean(pageResult, SuggestionRespVO.class);
        if (CollUtil.isEmpty(pageResulVO.getList())) {
            return success(pageResulVO);
        }
        // 读取用户信息，进行拼接
        Map<Long, MemberUserRespDTO> userMap = memberUserApi.getUserMap(convertSet(pageResult.getList(), SuggestionDO::getUserId));
        pageResulVO.getList().forEach(itemRespVO -> MapUtils.findAndThen(userMap, itemRespVO.getUserId(),
                userRespDTO -> {
                    itemRespVO.setNickname(userRespDTO.getNickname());
                    itemRespVO.setPhone(userRespDTO.getMobile());
                }));
        return success(pageResulVO);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出意见建议 Excel")
    @PreAuthorize("@ss.hasPermission('system:suggestion:export')")
    @OperateLog(type = EXPORT)
    public void exportSuggestionExcel(@Valid SuggestionPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SuggestionDO> list = suggestionService.getSuggestionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "意见建议.xls", "数据", SuggestionRespVO.class,
                        BeanUtils.toBean(list, SuggestionRespVO.class));
    }

}